﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using CommonLibrary.DataModel.Basic;
using System.Workflow.Runtime;
using ServiceApplication.Service.WorkFlow;
using System.Workflow.Activities;
using CommonLibrary.DataModel.WorkFlow;
using System.IO;
using System.Xml;
using Bukovics.Workflow.Hosting;
using CommonLibrary.DataModel.Form;
using ServiceApplication.Service.Basic;
using ServiceApplication.Service;

namespace AspWebTest
{
    public partial class WorkflowInsCreate : System.Web.UI.Page
    {
        WorkFlowService workflowService = new WorkFlowService();
        FormService formService = new FormService();
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button_OK_Click(object sender, EventArgs e)
        {
            //判断表单和wf模板对应变量是否匹配
            WfTemplateEntity Template = workflowService.QueryWfTemplate(int.Parse(ListBox_wftemplate.SelectedValue));
            FormEntity formEntity = formService.QueryFormEntity(int.Parse(ListBox_formtemplate.SelectedValue));
            List<string> args = new List<string>();
            foreach (var formfiledEntity in formEntity.formfieldEntityList)
            {
                if (formfiledEntity.ifwfjudge != "")
                    args.Add(formfiledEntity.ifwfjudge);
            }
            for (int i = 0; i < args.Count; i++)
            {
                if (!Template.wfrule.Contains(args[i]))
                {
                    MessageBox.Show(this, "工作流模板中未存在输入变量 " + args[i] + " 请确认");
                    return;
                }
            }


            string sql = "INSERT INTO wf_wf ([form_id] ,[wftype_id] ,[wftemplate_id] ,[name] ,[pos] ,[usever] ,[createtime] ,[lasttime] ," +
                "[creater_id] ,[creater_name] ,[lastuser_id] ,[lastuser_name] ,[users] ,[depts] ,[status] ,[descn] ) " +
                "VALUES (@form_id,@wftype_id,@wftemplate_id,@name,@pos,@usever,@createtime,@lasttime," +
                "@creater_id,@creater_name,@lastuser_id,@lastuser_name,@users,@depts,@status,@descn)";
            SqlParameter[] sqlParams = new SqlParameter[]{
                new SqlParameter("@form_id",ListBox_formtemplate.SelectedValue),
                new SqlParameter("@wftype_id",DropDownList_type.SelectedValue),
                new SqlParameter("@wftemplate_id",ListBox_wftemplate.SelectedValue),
                new SqlParameter("@name",TextBox_name.Text),
                new SqlParameter("@pos",TextBox_pos.Text),
                new SqlParameter("@usever",1),
                new SqlParameter("@createtime",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")),
                new SqlParameter("@lasttime",DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")),
                new SqlParameter("@creater_id",(Session[MyWorkHelpr.UserEntity] as UserEntity).id),
                new SqlParameter("@creater_name",(Session[MyWorkHelpr.UserEntity] as UserEntity).name),
                new SqlParameter("@lastuser_id",""),
                new SqlParameter("@lastuser_name",""),
                new SqlParameter("@users",""),
                new SqlParameter("@depts",""),
                new SqlParameter("@status",""),
                new SqlParameter("@descn",TextBox_descn.Text)
            };
            CommonLibrary.DB.DBExecuteUtil.executeSqlWithPar(sql, sqlParams);

            //新增加的wf模板加入到静态变量： Application[MyWorkHelpr.WorkflowStaticTemplate]
            WorkflowInstance ins = null;
            WorkflowRuntimeManager workflowRuntimeManager = Application[MyWorkHelpr.WorkflowRuntimeManager] as WorkflowRuntimeManager;
            StateMachineWorkflowInstance stateMachineWorkflowInstance = null;
            TextReader t1 = new StringReader(Template.wfdefinition);
            TextReader t2 = new StringReader(Template.wfrule);
            XmlReader define = XmlReader.Create(t1);
            XmlReader rule = XmlReader.Create(t2);
            ins = workflowRuntimeManager.WorkflowRuntime.CreateWorkflow(define, rule, null);
            stateMachineWorkflowInstance = new
                 StateMachineWorkflowInstance(workflowRuntimeManager.WorkflowRuntime, ins.InstanceId);
            (Application[MyWorkHelpr.WorkflowStaticTemplate] as Dictionary<int, StateMachineWorkflowInstance>).Add(Template.wftemplate_id, stateMachineWorkflowInstance);
     

            MessageBox.Show(this, "添加成功");
            Response.Redirect("Welcome.aspx");
        }
    }
}
